function get(obj, path, defaultValue = undefined) {
  const keys = path.split(".");
  let value = obj;
  for (const key of keys) {
    if (!value || typeof value !== "object") return defaultValue;
    value = value[key];
  }
  return value !== undefined ? value : defaultValue;
}

const obj = {
  user: {
    name: "John",
    age: 30,
    address: {
      city: "New York",
    },
  },
};

console.log(get(obj, "user.name")); // Output: 'John'
console.log(get(obj, "user.address.city")); // Output: 'New York'
console.log(get(obj, "user.gender", "Male")); // Output: 'Male' (default value)
console.log(get(obj, "user.address.postcode")); // Output: undefined
